<script>
	import Vue from 'vue'
	import { mapActions, mapState } from "vuex";

	export default {
		data() {
			return {
				data: {
					// 版本更新内容 支持<br>自动换行
					describe: '1. 修复已知问题<br>2. 优化用户体验',
					edition_url: '', //apk、wgt包下载地址或者应用市场地址  安卓应用市场 market://details?id=xxxx 苹果store itms-apps://itunes.apple.com/cn/app/xxxxxx
					edition_force: 1, //是否强制更新 0代表否 1代表是
					package_type: 0, //0是整包升级（apk或者appstore或者安卓应用市场） 1是wgt升级
					edition_issue: 1, //是否发行  0否 1是 为了控制上架应用市场审核时不能弹出热更新框
					edition_number: 100, //版本号 最重要的manifest里的版本号 （检查更新主要以服务器返回的edition_number版本号是否大于当前app的版本号来实现是否更新）
					edition_name: '1.0.0', // 版本名称 manifest里的版本名称
					edition_silence: 0 // 是否静默更新 0代表否 1代表是
				},
				platform: 'android'
			}
		},
		onLaunch: function() {
			this.changeBaseColor('#D279FF')
			


			// // #ifdef APP-PLUS

			// 		this.checkoutVersion()
			// 		// #endif
			const updateManager = uni.getUpdateManager()
			updateManager.onCheckForUpdate(function(res) {
				// 请求完新版本信息的回调
				console.log(res.hasUpdate)
			})
			// 下载新版本
			updateManager.onUpdateReady(function() {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success(res) {
						if (res.confirm) {
							// 重启应用
							updateManager.applyUpdate()
						}
					},
				})
			})
			// 新版本下载失败
			updateManager.onUpdateFailed(function(res) {
				// 新的版本下载失败
				uni.showModal({
					title: '已经有新版本了哟~',
					content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
				})
			})
		},


		onShow: function() {
			// this.checkoutVersion()
			console.log('App Show')
			// #ifdef APP-PLUS
			// APP从后台切换到前台，请求更新
			// 有些人APP从来不关，每次都需要调用接口重新校验版本
			// uni.getSystemInfo({
			// 		success: e => {							// 新版本更新校验
			// 			this.platform = e.platform	
			// 			this.checkoutVersion()
			// 	}
			// })
			// #endif



		},
		onHide: function() {
			console.log('App Hide')
		},
		mounted() {

			this.setAppInfo()
		},
		methods: {
			 ...mapActions(["changeBaseColor"]),

			checkoutVersion() {
				//	跟服务器上存放的的版本号（不是版本名称，版本号为 int 类型那个）比较，检查是否需要更新
				plus.runtime.getProperty(plus.runtime.appid, (info) => {
					console.log('info', info)
					// this.$api.checkVersion({
					// 	platform: this.platform == 'android' ? '045002' : '045001',
					// 	versionCode: parseFloat(info.versionCode)
					// }).then(res => {
					// 	if (res.code == 200 && res.result.update) {
					// 		需要更新
					// 		this.needUpdate = true
					// 		this.requestUpdateApp()
					// 	}
					// })
					version({
							type: this.platform == 'android' ? '1101' : '1102',

						})
						.then(res => {
							console.log(info.version)
							console.log(res.data.versionCode)
							var isbig = false
							isbig = this.compare(res.data.versionCode, info.version)

							console.log(isbig)
							if (isbig) {
								this.data.describe = res.data.versionInfo
								this.data.edition_number = info.version
								this.data.edition_name = info.version
								this.data.edition_url = res.data.downloadUrl
								console.log(this.data)

								//跳转更新页面 （注意！！！如果pages.json第一页的代码里有一打开就跳转其他页面的操作，下面这行代码最好写在setTimeout里面设置延时3到5秒再执行）
								uni.navigateTo({
									url: '/uni_modules/rt-uni-update/components/rt-uni-update/rt-uni-update?obj=' +
										JSON.stringify(this.data)
								});
								// this.requestUpdateApp(res.data.downloadUrl,res.data.versionInfo)
							}
							// if(res.data.versionCode){
							// 		console.log('info.version==res.data.versionCode',info.version==res.data.versionCode)
							// 	if(info.version==res.data.versionCode){

							// 	}else{
							// 		this.requestUpdateApp(res.data.downloadUrl,res.data.versionInfo)
							// 	}
							// }


						})
				});
			},
			// *
			// 	 * 版本号比较方法
			// 	 * 传入两个字符串，当前版本号：curV；比较版本号：reqV
			// 	 * 调用方法举例：compare("1.1","1.2")，将返回false
			// 	 */
			compare(curV, reqV) {
				if (curV && reqV) {
					//将两个版本号拆成数字
					var arr1 = curV.split('.'),
						arr2 = reqV.split('.');
					var minLength = Math.min(arr1.length, arr2.length),
						position = 0,
						diff = 0;
					//依次比较版本号每一位大小，当对比得出结果后跳出循环（后文有简单介绍）
					while (position < minLength && ((diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0)) {
						position++;
					}
					diff = (diff != 0) ? diff : (arr1.length - arr2.length);
					//若curV大于reqV，则返回true
					return diff > 0;
				} else {
					//输入为空
					console.log("版本号不能为空");
					return false;
				}
			},
			requestUpdateApp(downloadUrl, versionInfo) {
				console.log(downloadUrl, versionInfo)
				uni.showModal({
					title: '版本更新提示',
					content: versionInfo,
					confirmText: '更新',
					confirmColor: '#cb0725',
					success: res => {
						if (res.confirm) {
							console.log('点击更新')
							this.update(downloadUrl)
						}

						// 点击取消，退出APP
						if (res.cancel) {
							if (this.platform === 'android') {
								plus.runtime.quit();
							} else {
								plus.ios.import('UIApplication').sharedApplication().performSelector('exit');
							}
						}
					}
				});
			},
			update(downloadUrl) {
				uni.showLoading({
					title: '下载中'
				})

				let appUrl = downloadUrl;
				console.log('appUrl', appUrl)
				let downloadTask = uni.downloadFile({ //执行下载
					url: appUrl,
					success: downloadResult => {
						console.log('下载', downloadResult)
						uni.hideLoading()
						//下载成功，直接安装
						if (downloadResult.statusCode === 200) {
							plus.runtime.install( //安装
								downloadResult.tempFilePath, {
									force: true
								},
								function() {
									console.log("更新成功")
									// utils.showToast('更新成功，重启中');
									plus.runtime.restart();
								},
								function(e) {
									console.log("更新失败")
									// utils.showToast('更新失败');
								}
							);
						};
					},
				});
			},
			// 获取系统栏高度
			async setAppInfo() {
				let that = this
				return new Promise((resolve, reject) => {
					uni.getSystemInfo({
						success: function(e) {
							Vue.prototype.StatusBar = e.statusBarHeight
							// #ifdef H5
							Vue.prototype.CustomBar = e.statusBarHeight + 45
							// #endif

							// #ifdef APP-PLUS
							if (e.platform == 'android') {
								Vue.prototype.CustomBar = e.statusBarHeight + 50
							} else {
								Vue.prototype.CustomBar = e.statusBarHeight + 45
							}
							// #endif

							// #ifdef MP-WEIXIN
							let custom = wx.getMenuButtonBoundingClientRect()
							Vue.prototype.Custom = custom
							Vue.prototype.CustomBar = custom.bottom + custom.top - e
								.statusBarHeight
							// #endif
						},
					})
				})
			},

			// 自动登录
			async autoLogin(data) {},
		},
	}
</script>

<style lang="scss">
	page {
		// font-family: PingFangSC-Regular, PingFang SC;
		// height: 100%;


		background: #F7F7FB;
		padding-bottom: 120rpx;
	}


	/* #ifdef H5 */
	//修复H5底部导航挡住内容bug
	uni-app {
		height: auto;
	}

	//修复H5输入框上下不居中bug
	.uni-input-form {
		height: 100%;
	}

	//去除地图上高德地图标识符
	.amap-copyright {
		display: none !important;
	}

	.amap-logo {
		display: none !important;
	}

	.amap-ui-control-zoom {
		width: 60upx !important;
	}

	.amap-ui-control-zoom>* {
		width: 60upx !important;
		height: 60upx !important;
		line-height: 60upx !important;
	}

	.amap-ui-control-theme-dark {
		display: none !important;
	}

	/* #endif */

	.u-read-more__conten {
		text-indent: 0 !important;
	}

	.flex-row-flex-start {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
	}

	.flex-row-flex-start1 {
		display: flex;
		flex-direction: row;
		// align-items: center;
		justify-content: flex-start;
	}

	.flex-row-center {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.flex-column-center {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.flex-column-center1 {
		display: flex;
		flex-direction: column;
		// align-items: center;
		justify-content: center;
	}

	.flex-column-flex-start {
		display: flex;
		flex-direction: column;
		// align-items: center; 
		justify-content: flex-start;
	}

	.flex-column-flex-start1 {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: flex-start;
	}

	.flex-row-space-between {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	.flex-row-space-between1 {
		display: flex;
		flex-direction: row;
		// align-items: center;
		justify-content: space-between;
	}

	.flex-row-space-around {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-around;
	}

	.flex-column-space-between {
		display: flex;
		flex-direction: column;
		/* align-items: center; */
		justify-content: space-between;
	}

	.flex-column-space-between1 {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
	}

	.flex-row-flex-end {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-end;
	}

	.flex-column-flex-end {
		display: flex;
		flex-direction: column;
		vertical-align: center;
		justify-content: flex-end;
	}


	.display-flex {
		display: flex;
	}

	.flex-direction-row {
		flex-direction: row;
	}

	.flex-direction-column {
		flex-direction: column;
	}

	.justify-content-flex-start {
		justify-content: flex-start;
	}

	.justify-content-center {
		justify-content: center;
	}

	.align-items-flex-start {
		align-items: flex-start;
	}

	.align-items-center {
		align-items: center;
	}

	.justify-content-space-between {
		justify-content: space-between;
	}

	.justify-content-flex-end {
		justify-content: flex-end;
	}

	.vertical-align-center {
		vertical-align: center;
	}

	.BGImage {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		z-index: -1;
	}

	.flex1 {
		flex: 1
	}

	.flex-wrap {
		flex-wrap: wrap;
	}

	/* 其他 */
	.relative {
		position: relative;
	}

	.absolute {
		position: absolute;
	}

	.fixed {
		position: fixed;
	}


	.arrow {
		width: 24rpx;
		height: 24rpx;
	}

	.columnLine {
		margin-left: 30rpx;
		margin-right: 10rpx;
		width: 8rpx;
		height: 36rpx;
		background: #519EB0;
		border-radius: 8rpx;
		opacity: 1;
	}



	.columnTitle {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}

	.bottomView {
		height: 110rpx;
		background: #FFFFFF;
	}

	.tabIcon {
		width: 48rpx;
		height: 48rpx;
	}

	.tabBarTitle {
		font-size: 20rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #4E5885;
	}

	.tabBarSelected {

		color: #FFFFFF;
	}






	.flex-row-flex-start {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
	}

	.flex-row-flex-start1 {
		display: flex;
		flex-direction: row;
		// align-items: center;
		justify-content: flex-start;
	}

	.flex-row-center {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.flex-column-center {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.flex-column-center1 {
		display: flex;
		flex-direction: column;
		// align-items: center;
		justify-content: center;
	}

	.flex-column-flex-start {
		display: flex;
		flex-direction: column;
		// align-items: center; 
		justify-content: flex-start;
	}

	.flex-column-flex-start1 {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: flex-start;
	}

	.flex-row-space-between {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	.flex-row-space-around {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-around;
	}

	//平均分配
	.flex-column-space-evenly {
		display: flex;
		flex-direction: column;
		flex-wrap: wrap;
		justify-content: space-evenly;
		align-items: center;
		// justify-content: space-around;
	}

	.flex-column-space-between {
		display: flex;
		flex-direction: column;
		/* align-items: center; */
		justify-content: space-between;
	}

	.flex-row-flex-end {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-end;
	}

	.flex-column-flex-end {
		display: flex;
		flex-direction: column;
		vertical-align: center;
		justify-content: flex-end;
	}


	.display-flex {
		display: flex;
	}

	.flex-direction-row {
		flex-direction: row;
	}

	.flex-direction-column {
		flex-direction: column;
	}

	.justify-content-flex-start {
		justify-content: flex-start;
	}

	.justify-content-center {
		justify-content: center;
	}

	.align-items-flex-start {
		align-items: flex-start;
	}

	.align-items-center {
		align-items: center;
	}

	.justify-content-space-between {
		justify-content: space-between;
	}

	.justify-content-flex-end {
		justify-content: flex-end;
	}

	.vertical-align-center {
		vertical-align: center;
	}

	.show1 {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
	}

	.show2 {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
	}

	.BGImage {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		z-index: 1;
	}

	.flex1 {
		flex: 1
	}

	// .line{
	// 	width: 100%;
	// 	height: 2rpx;
	// 	background-color:  #C5C8FE ;
	// }
	.rightArrow {
		width: 11rpx;
		height: 19rpx;
	}

	.flex-wrap {
		flex-wrap: wrap;
	}

	/* 其他 */
	.relative {
		position: relative;
	}

	.absolute {
		position: absolute;
	}

	.fixed {
		position: fixed;
	}

	.fixed-top {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 99;
	}

	.fixed-bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 99;
	}


	.arrow {
		width: 24rpx;
		height: 24rpx;

	}

	.arrow1 {
		
		width: 12rpx;
		height: 12rpx;
	}

	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}

	.headerBar {
		padding-top: var(--status-bar-height);
	}

	.footer {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 99;
		// calc(var(--status-bar-height) + 60px);
		// padding-bottom: env(safe-area-inset-bottom);
		padding-bottom:calc( env(safe-area-inset-bottom)+ 20px);
	}

	.baseColor {
		color: $base-color;
	}

	.topM {
		 // calc(var(--status-bar-height) + 60px);
		margin-top: -194rpx;
	}

	.topH {
		height: 194rpx;
	}

	.wxz {
		width: 36rpx;
		height: 36rpx;
	}

	.overflow1 {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;

	}

	.overflow2 {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;

	}
	.bottomButton {
		margin: 30rpx;
		margin-bottom: 30rpx;
		height: 80rpx;
		background: linear-gradient( 141deg, #8CB3FD 0%, #3F81FF 100%);
		border-radius: 104rpx 104rpx 104rpx 104rpx;
		border-radius: 36rpx;
		opacity: 1;
		font-size: 30rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}
	.line{
		height: 1px;
		background: #F5F7FA;
	}
	.u-page__item__slot-icon{
		width: 40rpx;
		height: 40rpx;
	}
	.logo{
		width: 56rpx;
		height: 56rpx;
	}
	.topTitle{
		margin-left: 16rpx;
		font-weight: bold;
		font-size: 36rpx;
		color: #222222;
	}
	.topView{
		padding-top:180rpx;
		margin-top: -180rpx;
		width: 100%;
		height: 600rpx;
		background: linear-gradient( 180deg, #BBD8F6 0%, rgba(187,216,246,0) 100%);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}
</style>
